package com.wedimob.slideshare;

import java.net.URL;
import java.util.List;
import java.util.Observer;
import java.util.concurrent.TimeUnit;

import com.wedimob.slideshare.bo.Author;
import com.wedimob.slideshare.bo.Authors;
import com.wedimob.slideshare.bo.SharedDocument;
import com.wedimob.slideshare.bo.SharedDocuments;
import com.wedimob.slideshare.bo.Summaries;
import com.wedimob.slideshare.bo.Summary;
import com.wedimob.slideshare.task.PoolExecutor;
import com.wedimob.slideshare.task.WhatToDoAfter;
import com.wedimob.slideshare.task.network.TaskHtmlDownloader;

public class Search {

    static public SharedDocuments download(Summaries summaries, WriteType write, Observer o) {
        try {
            PoolExecutor pool = new PoolExecutor(10, o);
            for (Summary summary : summaries) {
                pool.execAsync(new TaskHtmlDownloader(pool, summary, write, 120));
            }

            pool.waitEnd(300, TimeUnit.SECONDS);

            SharedDocuments result = new SharedDocuments();
            result.addAll(pool.getResults().getResultByType(
                    SharedDocument.class));
            return result;

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    static public Authors searchAuthors(SearchCriteria criteria, WriteType write, Observer o) {
        try {

            criteria.setKeyWordType(KeyWordType.USER);
            PoolExecutor pool = new PoolExecutor(10, o);
            List<URL> urls = criteria.getURLs();
            for (URL url : urls)
                pool.execAsync(new TaskHtmlDownloader(pool, url,
                        WhatToDoAfter.EXTRACT_AUTHOR, true, write, criteria.getLimit(), 120));

            pool.waitEnd(300, TimeUnit.SECONDS);

            Authors result = new Authors();
            result.addAll(pool.getResults().getResultByType(Author.class));
            return result;

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    static public Summaries searchSummaries(SearchCriteria criteria,
            WriteType write, Observer o) {
        try {

            criteria.setKeyWordType(KeyWordType.CONTENT);
            PoolExecutor pool = new PoolExecutor(10, o);
            List<URL> urls = criteria.getURLs();
            for (URL url : urls)
                pool.execAsync(new TaskHtmlDownloader(pool, url,
                        WhatToDoAfter.EXTRACT_SUMMARY, false, write,
                        criteria.getLimit(), 120));

            pool.waitEnd(300, TimeUnit.SECONDS);

            Summaries result = new Summaries();
            result.addAll(pool.getResults().getResultByType(Summary.class));
            return result;

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    static public SharedDocuments searchDocuments(SearchCriteria criteria,
            WriteType write, Observer o) {
        try {

            criteria.setKeyWordType(KeyWordType.CONTENT);
            PoolExecutor pool = new PoolExecutor(10, o);
            List<URL> urls = criteria.getURLs();
            for (URL url : urls)
                pool.execAsync(new TaskHtmlDownloader(pool, url,
                        WhatToDoAfter.EXTRACT_SUMMARY, true, write,
                        criteria.getLimit(), 120));

            pool.waitEnd(300, TimeUnit.SECONDS);

            SharedDocuments result = new SharedDocuments();
            result.addAll(pool.getResults().getResultByType(
                    SharedDocument.class));
            return result;

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
